package com.rongshu.view.login.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.rongshu.R;
import com.rongshu.view.login.entity.PictureInfomation;

import java.io.File;
import java.util.List;

public class QuestionFeedbackAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int NORMAL = 0x01;
    public static final int IMAGE = 0x02;

    private List<PictureInfomation> pictureInfomationList = null;

    private Context mContext = null;

    //interface
    private OnNormalorImageClickListener mOnNormalorImageClickListener = null;

    public QuestionFeedbackAdapter(Context context) {
        this.mContext = context;
    }

    public void setPictureInfomationList(List<PictureInfomation> pictureInfomationList) {
        this.pictureInfomationList = pictureInfomationList;
    }

    public List<PictureInfomation> getPictureInfomationList() {
        return this.pictureInfomationList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == NORMAL) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_question_feed_back_normal_item, viewGroup, false);
            return new NormalHolder(view);
        } else if (viewType == IMAGE) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_question_feed_back_image_item, viewGroup, false);
            return new ImageHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        final int adapterPosition = viewHolder.getAdapterPosition();
        PictureInfomation pictureInfomation = pictureInfomationList.get(adapterPosition);
        if (viewHolder instanceof NormalHolder) {
            NormalHolder normalHolder = (NormalHolder) viewHolder;
            normalHolder.iv_upload_proof.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnNormalorImageClickListener.onClick(NORMAL, adapterPosition);
                }
            });
        } else if (viewHolder instanceof ImageHolder) {
            ImageHolder imageHolder = (ImageHolder) viewHolder;
            Glide.with(mContext).load(new File(pictureInfomation.getImage_path())).into(imageHolder.iv_upload_proof);
            imageHolder.iv_upload_proof.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnNormalorImageClickListener.onClick(IMAGE, adapterPosition);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if (pictureInfomationList.size() > 0) {
            return pictureInfomationList.size();
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (pictureInfomationList == null) {
            return super.getItemViewType(position);
        }
        int image_type = pictureInfomationList.get(position).getImage_type();
        switch (image_type) {
            case NORMAL:
                return NORMAL;
            case IMAGE:
                return IMAGE;
        }
        return super.getItemViewType(position);
    }

    public static class NormalHolder extends RecyclerView.ViewHolder {

        private ImageView iv_upload_proof;

        public NormalHolder(@NonNull View itemView) {
            super(itemView);
            iv_upload_proof = itemView.findViewById(R.id.iv_upload_proof);
        }
    }

    public static class ImageHolder extends RecyclerView.ViewHolder {

        private ImageView iv_upload_proof;

        public ImageHolder(@NonNull View itemView) {
            super(itemView);
            iv_upload_proof = itemView.findViewById(R.id.iv_upload_proof);
        }
    }


    public void setOnNormalorImageClickListener(OnNormalorImageClickListener listener) {
        this.mOnNormalorImageClickListener = listener;
    }

    //interface
    public interface OnNormalorImageClickListener {
        void onClick(int type, int position);
    }
}
